package View;


import database.BookData;

import javax.swing.*;
import javax.swing.plaf.synth.SynthEditorPaneUI;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.*;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.temporal.ChronoUnit;
import java.util.Vector;
import redis.clients.jedis.Jedis;


public class SearchBook extends JFrame {

    private String book;
    private String author;
    private int id;
    JLayeredPane laypane = new JLayeredPane();
    private DefaultTableModel model;
    private String select ;
    private JCheckBox box;
    private int selectrow;
    public int bookid;//书的编号
    public Date borrowtime;//借阅时间
    public Date returntime; //还书时间
    private String borrower;//借阅人
    public String lend;//是否在馆
    public String bookname;//书名
    private String bookname1;//书名1
    private String bookname2;//书名2
    DefaultTableModel model1;
    private String borroww;
    private String returnn;

    public void setName(String name) {
        this.borrower = name;

    }

    public void setModel(DefaultTableModel model1) {
        this.model1 = model1;
    }

    public SearchBook()  {

        /*
         * 查询图书
         */
        Font font1 = new Font("宋体",Font.BOLD,13);
        Font font2 = new Font("宋体",Font.BOLD,20);



        /*面板3中的标签*/
        Font font = new Font("宋体",Font.BOLD,20);
        JLabel lab3 = new JLabel("请选择查询方式：");
        lab3.setFont(font);
        lab3.setBounds(140, 60, 600, 30);


        /*面板3中的查询下拉框*/
        Dimension dimension = new Dimension(220,30);
        JComboBox<String> j = new JComboBox<>();
        j.setSize(dimension);
        j.addItem("按照书名查询");
        j.addItem("按照作者查询");
        j.addItem("按照编号查询");
        j.setBounds(140, 100,220, 30);
        j.setBackground(new Color(51,153,255) );
        j.setFont(font1);


        /*面板3中的搜索框*/
        JTextField text3 = new JTextField();
        Dimension dimension1 = new Dimension(270,30);
        text3.setFont(font1);
        text3.setSize(dimension1);
        text3.setBackground(new Color(51,153,255));
        text3.setBounds(390,100, 270, 30);

        /*按钮*/
        JButton button = new JButton("搜索");
        button.setBackground(new Color(51,153,255));
        button.setBounds(670, 100, 120, 30);
        button.setFont(font2);

        /*新建表格*/
        model = new DefaultTableModel();
        model.addColumn("书本编号", new Vector<Integer>());
        model.addColumn("书名",new Vector<String>());
        model.addColumn("作者",new Vector<String>());
        model.addColumn("是否在馆",new Vector<String>());
        model.addColumn("选择图书", new Vector<String>());
        JTable table = new JTable(model);

        JScrollPane jp = new JScrollPane(table);
        jp.setBounds(100, 150, 750, 450);

        /*显示出全部图书*/
        BookData bd = new BookData();
        try {

            bd.selectAll(model);
        } catch (Exception e2) {

            e2.printStackTrace();
        }


        JTableHeader head = table.getTableHeader();//创建表格表头对象
        head.setPreferredSize(new Dimension(head.getWidth(),35));//设置表头框框大小
        head.setFont(new Font("宋体",Font.BOLD,15));//设置表格字体
        table.setRowHeight(30);//设置表格行宽
        table.setFont(new Font("宋体", Font.PLAIN,13));//设置表格行中字体大小

        /*使单元格中的内容居中*/
        DefaultTableCellRenderer renderer=new DefaultTableCellRenderer();
        renderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        table.setDefaultRenderer(Object.class, renderer);

        /*添加复选框*/
        table.getColumnModel().getColumn(4).setCellRenderer(new TableCellRenderer() {

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value,
                                                           boolean isSelected, boolean hasFocus,int row, int column) {
                /*定义复选框*/
                box = new JCheckBox();
                /*使具有焦点的行对应的复选框被选中*/
                box.setSelected(isSelected);

                /*使复选框在表格中居中*/
                box.setHorizontalAlignment((int)0.5f);
                return box;
            }
        });



        JButton button1 = new JButton("还书");
        JButton button2 = new JButton("借阅");
        button1.setFont(font2);
        button2.setFont(font2);
        button1.setBackground(new Color(51,153,255));
        button1.setBounds(240, 630, 170, 40);
        button2.setBackground(new Color(51,153,255));
        button2.setBounds(520,630, 170, 40);

        /*把组件放在分层窗格里*/
        laypane.add(lab3, 150,1);
        laypane.add(j, 100,2);
        laypane.add(text3, 100,3);
        laypane.add(button, 100,4);
        laypane.add(jp, 200, 5);
        laypane.add(button1, 630,6);
        laypane.add(button2, 630,7);


        /*下拉框添加事件*/
        select = "按照书名查询";
        j.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {

                if(e.getStateChange()==ItemEvent.SELECTED) {
                    select =(String) e.getItem();

                }
            }
        });

        /*搜索监听*/
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                model.setRowCount(0);
                switch (select) {
                    case "按照书名查询":
                        book = text3.getText().trim();
                        try {

                            bd.bookSelect(model, book);
                        } catch (Exception e1) {

                            e1.printStackTrace();
                        }

                        break;
                    case "按照作者查询":
                        author = text3.getText().trim();
                        try {

                            bd.authorSelect(model, author);
                        } catch (Exception e1) {

                            e1.printStackTrace();
                        }

                        break;
                    case "按照编号查询":
                        id = Integer.parseInt(text3.getText().trim());
                        try {

                            bd.idSelect(model, id);
                        } catch (Exception e1) {

                        }
                        break;
                }
            }
        });

        /*选中行监听*/
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                selectrow = table.getSelectedRow();
                bookid =  (int) table.getValueAt(selectrow, 0);//获取所选书本编号
                bookname = (String) table.getValueAt(selectrow, 1);//获取所选书名
                lend= (String) table.getValueAt(selectrow, 3);//获取所选书本状态

            }
        });

        borroww = "借出";
        returnn = "退还";

        /*还书监听*/
        button1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(lend.equals("否")) {
                    bookname1 = "《" + bookname + "》";
                    try {
                        returntime = (Date) bd.booktime();
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }
                    //插入计算时间函数,redis存储时间
                    Jedis jedis = new Jedis("127.0.0.1",6379);
                    jedis.hset(bookname,"returntime", String.valueOf(returntime));
                    String d1 = jedis.hget(bookname,"returntime");
                    char rday1 = d1.charAt(8);
                    char rday2 = d1.charAt(9);
                    int  rnum1 = Integer.parseInt(String.valueOf(rday1));
                    int  rnum2 = Integer.parseInt(String.valueOf(rday2));
                    int rDay = rnum1+rnum2;

                    String d2 = jedis.hget(bookname,"borrowtime");
                    char bday1 = d2.charAt(8);
                    char bday2 = d2.charAt(9);
                    int  bnum1 = Integer.parseInt(String.valueOf(bday1));
                    int  bnum2 = Integer.parseInt(String.valueOf(bday2));
                    int bDay = bnum1+bnum2;
                    int alltime = rDay-bDay;
                    JOptionPane.showMessageDialog(null, bookname1+"退还成功,共借阅"+alltime+"天", "请去前台缴纳租金，谢谢您的使用", JOptionPane.PLAIN_MESSAGE);
                    try {
                        bd.updatereturn(model, bookname);
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    jedis.close();
                    /*借阅历史添加到数据库*/
                    try {
                        bd.addHistory(bookid, borrower, bookname, returntime, returnn);

                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }


                    /*更新借阅历史*/
                    try {
                        bd.showHistory(model1, borrower);
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            }
        });


        /*借书监听*/
        button2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(lend.equals("是")) {
                    /*获取借阅时间*/
                    try {
                        borrowtime = (Date) bd.booktime();
                    } catch (Exception e2) {
                        // TODO Auto-generated catch block
                        e2.printStackTrace();
                    }

                    /*更改在馆信息*/
                    bookname2 = "《" + bookname + "》";
                    JOptionPane.showMessageDialog(null, bookname2+"借阅成功", "感谢您的使用", JOptionPane.PLAIN_MESSAGE);
                    try {
                        bd.updatelend(model, bookname);
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    Jedis jedis = new Jedis();
                    jedis.hset(bookname,"borrowtime", String.valueOf(borrowtime));
                    jedis.close();


                    /*借阅历史添加到数据库*/
                    try {
                        bd.addHistory(bookid, borrower, bookname, borrowtime, borroww);

                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }


                    /*更新借阅历史*/
                    try {
                        bd.showHistory(model1, borrower);
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

                }else {
                    bookname2 = "《" + bookname + "》";
                    JOptionPane.showMessageDialog(null, bookname+"已被借阅", "抱歉", JOptionPane.PLAIN_MESSAGE);
                }

            }
        });




        laypane.setOpaque(false);

        text3.setOpaque(false);
    }

}
